using System;
using Key = Suake2.UI.client.Key;
namespace Suake2.UI.sys
{
	
	sealed public class JOGLKBD:KBD
	{
		//UPGRADE_ISSUE: Class 'java.awt.Robot' was not converted. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1000_javaawtRobot'"
		internal static Robot robot;
		public static InputListener listener = new InputListener();
		internal static System.Windows.Forms.Cursor emptyCursor = null;
		internal static System.Windows.Forms.Control c = null;
		
		internal static int win_w2 = 0;
		internal static int win_h2 = 0;
		
		public override void  Init()
		{
		}
		
		public override void  Update()
		{
			// get events
			HandleEvents();
		}
		
		public override void  Close()
		{
		}
		
		private void  HandleEvents()
		{
			int key;
			
			Jake2InputEvent event_Renamed;
			while ((event_Renamed = InputListener.nextEvent()) != null)
			{
				switch (event_Renamed.type)
				{
					
					case Jake2InputEvent.KeyPress: 
					case Jake2InputEvent.KeyRelease: 
						Do_Key_Event(XLateKey((System.Windows.Forms.KeyEventArgs) event_Renamed.ev), event_Renamed.type == Jake2InputEvent.KeyPress);
						break;
					
					
					case Jake2InputEvent.MotionNotify: 
						//					if (IN.ignorefirst) {
						//						IN.ignorefirst = false;
						//						break;
						//					}
						if (IN.mouse_active)
						{
							//UPGRADE_ISSUE: Method 'java.awt.event.MouseEvent.getX' was not converted. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1000_javaawteventMouseEventgetX'"
							mx = (((System.Windows.Forms.MouseEventArgs) event_Renamed.ev).getX() - win_w2) * 2;
							//UPGRADE_ISSUE: Method 'java.awt.event.MouseEvent.getY' was not converted. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1000_javaawteventMouseEventgetY'"
							my = (((System.Windows.Forms.MouseEventArgs) event_Renamed.ev).getY() - win_h2) * 2;
						}
						else
						{
							mx = 0;
							my = 0;
						}
						break;
						// see java.awt.MouseEvent
					
					case Jake2InputEvent.ButtonPress: 
						key = mouseEventToKey((System.Windows.Forms.MouseEventArgs) event_Renamed.ev);
						Do_Key_Event(key, true);
						break;
					
					
					case Jake2InputEvent.ButtonRelease: 
						key = mouseEventToKey((System.Windows.Forms.MouseEventArgs) event_Renamed.ev);
						Do_Key_Event(key, false);
						break;
					
					
					case Jake2InputEvent.WheelMoved: 
						int dir = ((MouseWheelEvent) event_Renamed.ev).getWheelRotation();
						if (dir > 0)
						{
							Do_Key_Event(Key.K_MWHEELDOWN, true);
							Do_Key_Event(Key.K_MWHEELDOWN, false);
						}
						else
						{
							Do_Key_Event(Key.K_MWHEELUP, true);
							Do_Key_Event(Key.K_MWHEELUP, false);
						}
						break;
					
					
					case Jake2InputEvent.CreateNotify: 
					case Jake2InputEvent.ConfigureNotify: 
						//UPGRADE_TODO: The method 'java.awt.event.ComponentEvent.getComponent' needs to be in a event handling method in order to be properly converted. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1171'"
						System.Windows.Forms.Control c = ((System.Windows.Forms.Control) ((System.EventArgs) event_Renamed.ev).getComponent());
						win_x = 0;
						win_y = 0;
						win_w2 = c.Width / 2;
						win_h2 = c.Height / 2;
						while (c != null)
						{
							if (c is System.Windows.Forms.Control)
							{
								System.Int32[] insets = SupportClass.GetInsets(((System.Windows.Forms.Control) c));
								win_x += insets[1];
								win_y += insets[0];
							}
							win_x += c.Location.X;
							win_y += c.Location.Y;
							c = c.Parent;
						}
						break;
					}
			}
			
			if (mx != 0 || my != 0)
			{
				// move the mouse to the window center again
				//UPGRADE_ISSUE: Method 'java.awt.Robot.mouseMove' was not converted. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1000_javaawtRobot'"
				robot.mouseMove(win_x + win_w2, win_y + win_h2);
			}
		}
		
		// strange button numbering in java.awt.MouseEvent
		// BUTTON1(left) BUTTON2(center) BUTTON3(right)
		// K_MOUSE1      K_MOUSE3        K_MOUSE2
		private int mouseEventToKey(System.Windows.Forms.MouseEventArgs ev)
		{
			switch (ev.getButton())
			{
				
				case MouseEvent.BUTTON3: 
					return Key.K_MOUSE2;
				
				case MouseEvent.BUTTON2: 
					return Key.K_MOUSE3;
				
				default: 
					return Key.K_MOUSE1;
				
			}
		}
		
		private static int XLateKey(System.Windows.Forms.KeyEventArgs ev)
		{
			
			int key = 0;
			//UPGRADE_ISSUE: Method 'java.awt.event.KeyEvent.getKeyCode' was not converted. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1000_javaawteventKeyEventgetKeyCode'"
			int code = ev.getKeyCode();
			
			switch (code)
			{
				
				//	00626                 case XK_KP_Page_Up:      key = K_KP_PGUP; break;
				case (int) System.Windows.Forms.Keys.PageUp:  key = Key.K_PGUP; break;
					
					//	00629                 case XK_KP_Page_Down: key = K_KP_PGDN; break;
				
				case (int) System.Windows.Forms.Keys.PageDown:  key = Key.K_PGDN; break;
					
					//	00632                 case XK_KP_Home: key = K_KP_HOME; break;
				
				case (int) System.Windows.Forms.Keys.Home:  key = Key.K_HOME; break;
					
					//	00635                 case XK_KP_End:  key = K_KP_END; break;
				
				case (int) System.Windows.Forms.Keys.End:  key = Key.K_END; break;
				
				
				case (int) System.Windows.Forms.Keys.Left:  key = Key.K_KP_LEFTARROW; break;
				
				case (int) System.Windows.Forms.Keys.Left:  key = Key.K_LEFTARROW; break;
				
				
				case (int) System.Windows.Forms.Keys.Right:  key = Key.K_KP_RIGHTARROW; break;
				
				case (int) System.Windows.Forms.Keys.Right:  key = Key.K_RIGHTARROW; break;
				
				
				case (int) System.Windows.Forms.Keys.Down:  key = Key.K_KP_DOWNARROW; break;
				
				case (int) System.Windows.Forms.Keys.Down:  key = Key.K_DOWNARROW; break;
				
				
				case (int) System.Windows.Forms.Keys.Up:  key = Key.K_KP_UPARROW; break;
				
				case (int) System.Windows.Forms.Keys.Up:  key = Key.K_UPARROW; break;
				
				
				case (int) System.Windows.Forms.Keys.Escape:  key = Key.K_ESCAPE; break;
				
				
				
				case (int) System.Windows.Forms.Keys.Enter:  key = Key.K_ENTER; break;
					//	00652                 case XK_KP_Enter: key = K_KP_ENTER;     break;
				
				
				case (int) System.Windows.Forms.Keys.Tab:  key = Key.K_TAB; break;
				
				
				case (int) System.Windows.Forms.Keys.F1:  key = Key.K_F1; break;
				
				case (int) System.Windows.Forms.Keys.F2:  key = Key.K_F2; break;
				
				case (int) System.Windows.Forms.Keys.F3:  key = Key.K_F3; break;
				
				case (int) System.Windows.Forms.Keys.F4:  key = Key.K_F4; break;
				
				case (int) System.Windows.Forms.Keys.F5:  key = Key.K_F5; break;
				
				case (int) System.Windows.Forms.Keys.F6:  key = Key.K_F6; break;
				
				case (int) System.Windows.Forms.Keys.F7:  key = Key.K_F7; break;
				
				case (int) System.Windows.Forms.Keys.F8:  key = Key.K_F8; break;
				
				case (int) System.Windows.Forms.Keys.F9:  key = Key.K_F9; break;
				
				case (int) System.Windows.Forms.Keys.F10:  key = Key.K_F10; break;
				
				case (int) System.Windows.Forms.Keys.F11:  key = Key.K_F11; break;
				
				case (int) System.Windows.Forms.Keys.F12:  key = Key.K_F12; break;
				
				
				case (int) System.Windows.Forms.Keys.Back:  key = Key.K_BACKSPACE; break;
				
				
				case (int) System.Windows.Forms.Keys.Delete:  key = Key.K_DEL; break;
					//	00683                 case XK_KP_Delete: key = K_KP_DEL; break;
				
				
				case (int) System.Windows.Forms.Keys.Pause:  key = Key.K_PAUSE; break;
				
				
				case (int) System.Windows.Forms.Keys.ShiftKey:  key = Key.K_SHIFT; break;
				
				case (int) System.Windows.Forms.Keys.ControlKey:  key = Key.K_CTRL; break;
				
				
				case (int) System.Windows.Forms.Keys.Menu: 
				//UPGRADE_ISSUE: Field 'java.awt.event.KeyEvent.VK_ALT_GRAPH' was not converted. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1000_javaawteventKeyEventVK_ALT_GRAPH_F'"
				case KeyEvent.VK_ALT_GRAPH:  key = Key.K_ALT; break;
					
					//	00700                 case XK_KP_Begin: key = K_KP_5; break;
					//	00701
				
				case (int) System.Windows.Forms.Keys.Insert:  key = Key.K_INS; break;
					// toggle console for DE and US keyboards
				
				//UPGRADE_ISSUE: Field 'java.awt.event.KeyEvent.VK_DEAD_ACUTE' was not converted. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1000_javaawteventKeyEventVK_DEAD_ACUTE_F'"
				case KeyEvent.VK_DEAD_ACUTE: 
				//UPGRADE_ISSUE: Field 'java.awt.event.KeyEvent.VK_CIRCUMFLEX' was not converted. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1000_javaawteventKeyEventVK_CIRCUMFLEX_f'"
				case KeyEvent.VK_CIRCUMFLEX: 
				//UPGRADE_ISSUE: Field 'java.awt.event.KeyEvent.VK_DEAD_CIRCUMFLEX' was not converted. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1000_javaawteventKeyEventVK_DEAD_CIRCUMFLEX_F'"
				case KeyEvent.VK_DEAD_CIRCUMFLEX:  key = '`'; break;
				
				
				default: 
					//UPGRADE_TODO: The equivalent in .NET for method 'java.awt.event.KeyEvent.getKeyChar' may return a different value. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1043'"
					//UPGRADE_ISSUE: Method 'java.awt.event.KeyEvent.getKeyChar' was not converted. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1000_javaawteventKeyEventgetKeyChar'"
					key = ev.getKeyChar();
					
					if (key >= 'A' && key <= 'Z')
						key = key - 'A' + 'a';
					break;
				
			}
			if (key > 255)
				key = 0;
			
			return key;
		}
		
		public override void  Do_Key_Event(int key, bool down)
		{
			Key.Event(key, down, Timer.Milliseconds());
		}
		
		public void  centerMouse()
		{
			//UPGRADE_ISSUE: Method 'java.awt.Robot.mouseMove' was not converted. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1000_javaawtRobot'"
			robot.mouseMove(win_x + win_w2, win_y + win_h2);
		}
		
		public override void  installGrabs()
		{
			if (emptyCursor == null)
			{
				//UPGRADE_TODO: Class 'javax.swing.ImageIcon' was converted to 'System.Drawing.Image' which has a different behavior. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1073_javaxswingImageIcon'"
				//UPGRADE_ISSUE: Constructor 'javax.swing.ImageIcon.ImageIcon' was not converted. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1000_javaxswingImageIconImageIcon_byte[]'"
				System.Drawing.Image emptyIcon = new ImageIcon(new sbyte[0]);
				//UPGRADE_ISSUE: Method 'java.awt.Toolkit.createCustomCursor' was not converted. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1000_javaawtToolkit'"
				//UPGRADE_ISSUE: Method 'java.awt.Component.getToolkit' was not converted. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1000_javaawtComponentgetToolkit'"
				emptyCursor = c.getToolkit().createCustomCursor(emptyIcon, new System.Drawing.Point(0, 0), "emptyCursor");
			}
			c.Cursor = emptyCursor;
			centerMouse();
		}
		
		public override void  uninstallGrabs()
		{
			//UPGRADE_ISSUE: Member 'java.awt.Cursor.getPredefinedCursor' was converted to 'System.Windows.Forms.Cursor' which cannot be assigned to an int. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1086'"
			//UPGRADE_ISSUE: Member 'java.awt.Cursor.DEFAULT_CURSOR' was converted to 'System.Windows.Forms.Cursors.Default' which cannot be assigned to an int. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1086'"
			c.Cursor = System.Windows.Forms.Cursors.Default;
		}
		static JOGLKBD()
		{
			{
				try
				{
					//UPGRADE_ISSUE: Constructor 'java.awt.Robot.Robot' was not converted. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1000_javaawtRobot'"
					robot = new Robot();
				}
				catch (System.Exception e)
				{
					System.Environment.Exit(1);
				}
			}
		}
	}
}